class AIAssistant:
    def __init__(self, id, name, type, version, online, score):
        self.id = id
        self.name = name
        self.type = type
        self.version = version
        self.online = online
        self.score = score

    def __str__(self):
        status = "🟢在线" if self.online else "🔴离线"
        return f"ID: {self.id}, 名称: {self.name}, 类型: {self.type}, 版本: {self.version}, 状态: {status}, 能力评分: {self.score} [{self.type}]"

    def to_dict(self):
        return {
            "id": self.id,
            "name": self.name,
            "type": self.type,
            "version": self.version,
            "online": self.online,
            "score": self.score
        }


class TextAssistant(AIAssistant):
    def __init__(self, id, name, version, online, score, language="中文"):
        super().__init__(id, name, "文本助手", version, online, score)
        self.language = language


class ImageAssistant(AIAssistant):
    def __init__(self, id, name, version, online, score, resolution="1024x1024"):
        super().__init__(id, name, "图像助手", version, online, score)
        self.resolution = resolution


class DataAnalysisAssistant(AIAssistant):
    def __init__(self, id, name, version, online, score, algorithm="决策树"):
        super().__init__(id, name, "数据分析助手", version, online, score)
        self.algorithm = algorithm


class TranslationAssistant(AIAssistant):
    def __init__(self, id, name, version, online, score, languages="中英互译"):
        super().__init__(id, name, "翻译助手", version, online, score)
        self.languages = languages


class VoiceAssistant(AIAssistant):
    def __init__(self, id, name, version, online, score, voice_type="女声"):
        super().__init__(id, name, "语音助手", version, online, score)
        self.voice_type = voice_type


class AIManager:
    def __init__(self, max_size=100):
        self.assistants = []
        self.max_size = max_size

    def add_assistant(self, assistant):
        if len(self.assistants) >= self.max_size:
            print("❌ 助手数量已达上限，无法添加新助手")
            return False

        for a in self.assistants:
            if a.id == assistant.id:
                print("❌ 助手ID已存在")
                return False

        self.assistants.append(assistant)
        print(f"✅ 助手【{assistant.name}】添加成功！")
        return True

    def get_all_assistants(self):
        return self.assistants

    def get_assistant_by_id(self, id):
        for assistant in self.assistants:
            if assistant.id == id:
                return assistant
        return None

    def update_assistant(self, id, field, value):
        assistant = self.get_assistant_by_id(id)
        if not assistant:
            print("❌ 助手不存在")
            return False

        if hasattr(assistant, field):
            if field == "online":
                value = True if value == "1" else False
            elif field == "score":
                value = int(value)

            setattr(assistant, field, value)
            print(f"✅ 助手【{id}】修改成功！")
            return True
        else:
            print("❌ 属性不存在")
            return False

    def delete_assistant(self, id):
        assistant = self.get_assistant_by_id(id)
        if not assistant:
            print("❌ 助手不存在")
            return False

        self.assistants = [a for a in self.assistants if a.id != id]
        print(f"✅ 助手【{assistant.name}】已删除。")
        return True

    def start_service(self, id):
        assistant = self.get_assistant_by_id(id)
        if not assistant:
            print("❌ 助手不存在")
            return False

        if assistant.online:
            print(f"助手【{assistant.name}】已在运行。")
        else:
            assistant.online = True
            print(f"✅ 助手【{assistant.name}】已启动。")
        return True

    def stop_service(self, id):
        assistant = self.get_assistant_by_id(id)
        if not assistant:
            print("❌ 助手不存在")
            return False

        if not assistant.online:
            print(f"助手【{assistant.name}】已停止。")
        else:
            assistant.online = False
            print(f"✅ 助手【{assistant.name}】已停止。")
        return True

    def assign_task(self, id, task):
        assistant = self.get_assistant_by_id(id)
        if not assistant:
            print("❌ 助手不存在")
            return False

        if not assistant.online:
            print(f"❌ {assistant.name} 当前离线，无法执行任务。")
            return False

        print(f"✅ {assistant.name} 正在执行任务: {task}")
        return True

    def get_statistics(self):
        total = len(self.assistants)
        online_count = sum(1 for a in self.assistants if a.online)
        offline_count = total - online_count
        avg_score = sum(a.score for a in self.assistants) / total if total > 0 else 0

        return {
            "total": total,
            "online": online_count,
            "offline": offline_count,
            "avg_score": avg_score
        }


def print_menu():
    print("\n" + "=" * 10 + " AI助手管理器 " + "=" * 10)
    print("1. 查看所有AI助手列表")
    print("2. 根据ID查询助手详情")
    print("3. 添加新的AI助手")
    print("4. 根据ID修改助手信息")
    print("5. 删除AI助手（按ID）")
    print("6. 启动AI助手服务")
    print("7. 停止AI助手服务")
    print("8. 为AI助手分配任务")
    print("9. 显示系统统计信息")
    print("0. 退出系统")
    print("=" * 40)


def create_assistant_interactive():
    print("添加新AI助手")

    id = input("请输入ID: ").strip()
    name = input("请输入名称: ").strip()

    print("请选择助手类型:")
    print("1. 文本助手")
    print("2. 图像助手")
    print("3. 数据分析助手")
    print("4. 翻译助手")
    print("5. 语音助手")

    type_choice = input("请选择(1-5): ").strip()
    type_map = {
        "1": "文本助手",
        "2": "图像助手",
        "3": "数据分析助手",
        "4": "翻译助手",
        "5": "语音助手"
    }

    if type_choice not in type_map:
        print("❌ 无效的类型选择")
        return None

    assistant_type = type_map[type_choice]
    version = input("请输入版本号: ").strip()
    online = input("是否在线？(1-是, 0-否): ").strip()
    online = True if online == "1" else False
    score = int(input("请输入能力评分(0-100): ").strip())

    if assistant_type == "文本助手":
        return TextAssistant(id, name, version, online, score)
    elif assistant_type == "图像助手":
        return ImageAssistant(id, name, version, online, score)
    elif assistant_type == "数据分析助手":
        return DataAnalysisAssistant(id, name, version, online, score)
    elif assistant_type == "翻译助手":
        return TranslationAssistant(id, name, version, online, score)
    elif assistant_type == "语音助手":
        return VoiceAssistant(id, name, version, online, score)


def main():
    manager = AIManager()

    # 添加一些示例数据
    manager.add_assistant(VoiceAssistant("101", "小智", "v1.2", True, 85))
    manager.add_assistant(TextAssistant("102", "文小助", "v2.0", True, 92))
    manager.add_assistant(ImageAssistant("103", "图灵", "v1.5", False, 88))
    manager.add_assistant(DataAnalysisAssistant("104", "数小析", "v1.0", True, 79))
    manager.add_assistant(TranslationAssistant("105", "翻译精灵", "v2.1", False, 95))

    print("欢迎使用 AI助手管理器 v1.0")

    while True:
        print_menu()
        choice = input("请选择操作: ").strip()

        if choice == "1":
            print("所有AI助手列表：")
            assistants = manager.get_all_assistants()
            for i, assistant in enumerate(assistants, 1):
                print(f"{i}. {assistant}")

        elif choice == "2":
            id = input("请输入要查询的AI助手ID: ").strip()
            assistant = manager.get_assistant_by_id(id)
            if assistant:
                print(f"✅ 查询结果: {assistant}")
            else:
                print("❌ 助手不存在")

        elif choice == "3":
            assistant = create_assistant_interactive()
            if assistant:
                manager.add_assistant(assistant)

        elif choice == "4":
            id = input("请输入ID: ").strip()
            print("可修改属性列表为：name, type, version, online, score")
            field_value = input("请输入要修改的属性和值，用空格分割: ").strip().split()
            if len(field_value) == 2:
                field, value = field_value
                manager.update_assistant(id, field, value)
            else:
                print("❌ 输入格式错误")

        elif choice == "5":
            id = input("请输入要删除的AI助手ID: ").strip()
            manager.delete_assistant(id)

        elif choice == "6":
            id = input("请输入要启动的AI助手ID: ").strip()
            manager.start_service(id)

        elif choice == "7":
            id = input("请输入要停止的AI助手ID: ").strip()
            manager.stop_service(id)

        elif choice == "8":
            id = input("请输入目标AI助手ID: ").strip()
            task = input("请输入任务内容: ").strip()
            manager.assign_task(id, task)

        elif choice == "9":
            stats = manager.get_statistics()
            print("系统统计信息：")
            print(f"总助手数: {stats['total']}")
            print(f"在线助手数: {stats['online']}")
            print(f"离线助手数: {stats['offline']}")
            print(f"平均能力评分: {stats['avg_score']:.2f}")

        elif choice == "0":
            print("感谢使用AI助手管理器，再见！")
            break

        else:
            print("❌ 无效选择，请重新输入")


if __name__ == "__main__":
    main()